
if { ! [ info exists env(XSMACK_BIN) ] } {
    puts "Set XSMACK_BIN"
    exit 1
}

set xsmack_home $env(XSMACK_HOME)
set xsmack_bin  $env(XSMACK_BIN)
source $xsmack_bin/xsmack-lib.tcl

proc usage { } {
    puts "usage: categories <USER> <LOCATION>"
}

if { $argc != 2 } {
    usage
    exit 1
}

set user     [ lindex $argv 0 ]
set location [ lindex $argv 1 ]

# Settings directory:
set settings $xsmack_home/users/$user/settings

array_read_one_plus_comments $settings/categories.txt categories
array_read_one_plus_comments $settings/locations.txt locations

set counts [ dict create ]
set total 0
set exercises 0

foreach k [ array names categories ] { 
    if { [ string first $location $locations($k) ] >= 0 } { 
        incr exercises
        foreach c $categories($k) { 
            dict incr counts $c 
            incr total
        }}}

puts "exercises: $exercises"
puts "total:     $total"
dict for { k v } $counts { 
    set rate [ expr round(100 * double($v) / $exercises) ]
    printf "%-5s %2i %2.f%%" $k $v $rate 
}
